import { injectable } from '@/services/service'
import { emit } from '@tauri-apps/api/event'
import { invoke } from '@tauri-apps/api/tauri'

/** 准星配置 */
export interface CrossConfig {
  /** 大小 */
  size: number
  /** 线条宽度 */
  width: number
  /** 中心空白 */
  gap: number
  /** 颜色 */
  color: string,
  /** x偏移 */
  offsetX: number,
  /** y偏移 */
  offsetY: number
}

export interface Usable {
  /** 右键后 y 方向偏移 */
  y_offset: number,
  /** 使用时长 毫秒 */
  time_cast: number
  /** 快捷键 0 8 9 */
  key: string,
  /** 重复次数 */
  repeat: number,
}

export interface Food extends Usable {
  /** 食物分类 0 卡路里 1 水 2 蛋白质 */
  category: number,
}

/** 挂机配置 */
export interface HugConfig {
  // 类型 0 小推车 1 爬 2 咋箱子
  type: number,
  /**
   * 额外参数
   * `type==0` 
   * `type==1` 0 蹲着 1 趴着 趴着不能是拿着箱子
   * `type==0` 
   */
  sub_type: number,
  // 吃东西方式 0 背包快捷键 1 箱子中
  eat_type: number,
  foods: Food[],
  /** 鞋子 */
  shoe: Usable & {
    /** 是否启用维修 */
    enable: boolean,
    /** 维修间隔 分钟 */
    interval: number
  },
  /** 手套 */
  glove: Usable & { enable: boolean, interval: number },
  /** 缝纫工具所在列 */
  toolkits: number[]
}

const CROSS_KEY = '__CROSS_CONFIG__'
const HUG_KEY = '__HUG_CONFIG__'
const HUGS_KEY = '__HUGS_CONFIG__'

@injectable
/** 设置服务 */
export default class SettingService {
  crossConfig = {} as CrossConfig
  hugConfig = {} as HugConfig

  settings = [] as HugConfig[]

  constructor() {
    try {
      this.crossConfig = JSON.parse(localStorage.getItem(CROSS_KEY) as string || '-')
    } catch {
      this.crossConfig = {
        size: 10,
        width: 2,
        gap: 2,
        offsetX: 8,
        offsetY: 7,
        color: '#62FF00FF'
      }
    }
    try {
      this.hugConfig = JSON.parse(localStorage.getItem(HUG_KEY) as string || '-')
      this.hugConfig.sub_type = this.hugConfig.sub_type || 0
      if (!Array.isArray(this.hugConfig.toolkits)) {
        this.hugConfig.toolkits = [this.hugConfig.toolkits, 0]
      }

      this.settings = JSON.parse(localStorage.getItem(HUGS_KEY) as string || '[]')
      this.settings.forEach(v => {
        if (!v.toolkits) {
          v.toolkits = [0, 0]
        } else if (!Array.isArray(v.toolkits)) {
          v.toolkits = [v.toolkits, 0]
        }
      })
    } catch {
      this.hugConfig = {
        type: 0,
        sub_type: 0,
        eat_type: 0,
        foods: [
          {
            category: 0,
            key: '8',
            time_cast: 4000,
            repeat: 3,
            y_offset: 0
          },
          {
            category: 1,
            key: '0',
            time_cast: 4000,
            repeat: 5,
            y_offset: 0
          },
          {
            category: 2,
            key: '9',
            time_cast: 11000,
            repeat: 2,
            y_offset: 0
          },
        ],
        shoe: {
          key: '6',
          time_cast: 4500,
          repeat: 1,
          y_offset: 57,
          enable: true,
          interval: 5
        },
        glove: {
          key: '5',
          time_cast: 4500,
          repeat: 1,
          y_offset: 76,
          enable: true,
          interval: 5
        },
        toolkits: [0, 0]
      }
      this.settings = [
        this.hugConfig
      ]
    }
  }

  saveCorss(cfg: CrossConfig) {
    try {
      localStorage.setItem(CROSS_KEY, JSON.stringify(cfg))
      if (JSON.stringify(cfg) !== JSON.stringify(this.hugConfig)) {
        emit('setting-change', { cross: cfg })
        this.crossConfig = cfg
      }
    } catch {}
  }

  saveHug(cfg: HugConfig) {
    try {
      localStorage.setItem(HUG_KEY, JSON.stringify(cfg))
      this.hugConfig = cfg
      
      const opts = JSON.parse(JSON.stringify(this.hugConfig)) as Any
      opts.foods.forEach((v: Any) => v.key = +v.key)
      opts.shoe.key = +opts.shoe.key
      opts.glove.key = +opts.glove.key
      opts.toolkits = opts.toolkits.filter((v: number) => v)
      invoke('update_hug_setting', { opts })

      const _config = JSON.parse(JSON.stringify(cfg))
      const index = this.settings.findIndex(v => v.type === _config.type)
      if (index > -1) {
        this.settings[index] = _config
      } else {
        this.settings.push(_config)
      }
      console.log(this.settings)
      localStorage.setItem(HUGS_KEY, JSON.stringify(this.settings))
    } catch {}
  }

  resetHug() {
    try {
      localStorage.removeItem(HUG_KEY)
      localStorage.removeItem(HUGS_KEY)
      throw ''
    } catch {
      this.hugConfig = {
        type: 0,
        sub_type: 0,
        eat_type: 0,
        foods: [
          {
            category: 0,
            key: '8',
            time_cast: 4000,
            repeat: 3,
            y_offset: 0
          },
          {
            category: 1,
            key: '0',
            time_cast: 4000,
            repeat: 5,
            y_offset: 0
          },
          {
            category: 2,
            key: '9',
            time_cast: 11000,
            repeat: 2,
            y_offset: 0
          },
        ],
        shoe: {
          key: '6',
          time_cast: 4500,
          repeat: 1,
          y_offset: 57,
          enable: true,
          interval: 5
        },
        glove: {
          key: '5',
          time_cast: 4500,
          repeat: 1,
          y_offset: 76,
          enable: true,
          interval: 5
        },
        toolkits: [0, 0]
      }
      this.settings = [
        this.hugConfig
      ]
    }
  }
}